package com.cictec.middleware.gps.engine.position;

import java.text.DecimalFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.gps.Point;
import com.cictec.middleware.core.entity.Bus;
import com.cictec.middleware.core.entity.BusLine;
import com.cictec.middleware.core.entity.BusLineStation;
import com.cictec.middleware.core.pojo.MiddlewarePosition;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.TimingPoint;
import com.cictec.middleware.gps.service.BusLineService;

/**
 * GPS位置信息对象构建器
 * @author Administrator
 *
 */
@Component
public class TimingPointBuilder {
	
	@Autowired
	private BusLineService busLineService;

	
	public TimingPoint builderPosition(MiddlewarePosition position, BusLine busLine, Point hxPoint, BusInstance busInstance) {

		// 将数据转换为TimingObject
		TimingPoint timingPoint = new TimingPoint();
		timingPoint.setTime(position.getCreateTime()); // 数据发送时间【也是数据采集时间】
		
		timingPoint.setLoginStatus(Constants.VAL_LOGIN_ONLINE);// 设置设备在线
		timingPoint.setUploadTime(DateUtils.formatDateTimes(position.getSendTime())); // 数据上传时间
		timingPoint.setGatherTime(DateUtils.formatDateTimes(position.getCreateTime()));// 数据采集时间
		timingPoint.setDevCode(position.getDevCode());// 获取设备编号
		timingPoint.setDevUuid(busInstance.getDevice().getDevUuid());// 获取设备uuid

		// 设置线路信息
		timingPoint.setDevLineId(String.valueOf(position.getLineId())); // 设备配置的线路号
		timingPoint.setLineUuid(busLine.getLineUuid()); // 线路uuid
		timingPoint.setLineName(busLine.getLineName()); // 线路名称

		// 设置车辆信息
		Bus bus = busInstance.getBus();
		timingPoint.setBusUuid(bus.getBusUuid()); // 车辆uuid
		timingPoint.setBusNumber(bus.getBusPlateNumber()); // 车牌号
		if (null != bus.getBusSelfCode()) {
			timingPoint.setBusSelfCode(bus.getBusSelfCode()); // 车辆自编号
		}

		/* ****坐标转换 start *******/
		timingPoint.setLatitude(String.valueOf(hxPoint.getY()));
		timingPoint.setLongitude(String.valueOf(hxPoint.getX()));
		timingPoint.setLatitudeIndicator("N");
		timingPoint.setLongitudeIndicator("E");
		/* ****坐标转换 end *******/

		// 速度
		Double kilSpeed = (double) position.getSpeed(); //
		DecimalFormat df = new DecimalFormat("###,##0.00");
		String speed = df.format(kilSpeed);
		timingPoint.setSpeed(speed);

		// 方向角
		int direction = position.getAngle() * 2;
		timingPoint.setAttribute(Constants.MSG_INFO_POSITION_COURSE, String.valueOf(direction));
		timingPoint.setCounrse(String.valueOf(direction));
		
		return timingPoint;

	}

	public TimingPoint builderEnterOutPosition(MiddlewarePosition position, BusLine busLine, Point hxPoint, BusInstance busInstance) {
		TimingPoint timingPoint = builderPosition(position, busLine, hxPoint, busInstance);

		
		timingPoint.setStationEnterOut(String.valueOf(position.getEnterOutStatus()));
		timingPoint.setStationEnterOutTrigger(String.valueOf(position.getTriggerType()));
		
		// 原始进出站站序号
		BusLineStation orglineStation;
		// 添加车辆所在站点，进站为当前站点，出站为下一个站点
		BusLineStation lineStation;
		if (busLine.getLineType().equals("0")) {
			orglineStation = busLineService.getLineStationByStationSeq(busLine.getLineUuid(), position.getPositionNumber());

			if (position.getEnterOutStatus() == 1) {
				// 进站为当前站点
				lineStation = busLineService.getLineStationByStationSeq(busLine.getLineUuid(), position.getPositionNumber());
			} else {
				// 出站为下一个站点
				lineStation = busLineService.getLineStationByStationSeq(busLine.getLineUuid(), position.getPositionNumber() + 1);
				if (lineStation == null) {
					lineStation = busLineService.getLineStationByStationSeq(busLine.getLineUuid(), 1);
				}
			}
		} else {
			orglineStation = busLineService.getLineStationByStationSeq(busLine.getLineUuid(), busLine.getLineType(), position.getPositionNumber());
			if (position.getEnterOutStatus() == 1) {
				// 进站为当前站点
				lineStation = busLineService.getLineStationByStationSeq(busLine.getLineUuid(), busLine.getLineType(), position.getPositionNumber());
			} else {
				// 出站为下一个站点
				lineStation = busLineService.getLineStationByStationSeq(busLine.getLineUuid(), busLine.getLineType(), position.getPositionNumber() + 1);
				if (lineStation == null) {
					lineStation = busLineService.getLineStationByStationSeq(busLine.getLineUuid(), busLine.getLineType(), 1);
				}
			}
		}

		if (orglineStation != null) {
			timingPoint.setStationLineSeqOrg(PMSUtils.isNull(orglineStation.getLsSequence()));
			timingPoint.setStationLineTypeOrg(orglineStation.getLsLineType());
			timingPoint.setStationNameOrg(orglineStation.getStaName());
			timingPoint.setStationUuidOrg(orglineStation.getLsUuid());
		}

		if (lineStation != null) {
			
			timingPoint.setAttributeStandBy(Constants.MSG_INFO_POSITION_STANDBY_FLAG_0);// 设置非代发区
			timingPoint.setAttributeDeviate(Constants.VAL_IS_PX_0);	// 设置不偏线
			timingPoint.setAttributeDrift(Constants.VAL_IS_PX_0);// 设置坐标不漂移
			timingPoint.setLineType(lineStation.getLsLineType());	// 设置线路上下行
			timingPoint.setStationUuid(lineStation.getLsUuid());
			timingPoint.setStationName(lineStation.getStaName());
			timingPoint.setStationSeq(String.valueOf(lineStation.getLsSequence()));// 设置所在站点序号

			timingPoint.setAttribute("dataTypeTME", "TME");
		}
		return timingPoint;
	}

}
